package com.yanqu.road.server.cmd.activity.gourdchild;

import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.GourdChildProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.gourdchild.CrossGourdChildMgr;
import com.yanqu.road.server.manager.gourdchild.GourdChildActivity;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = CrossProtocol.C_GOURD_CHILD_ACTIVITY_DRAW_HIDE_BOX, desc = "抽隐藏盲盒")
public class CrossGourdChildDrawHideBoxCmd extends CrossCommand {

    int activityId;
    long serverId;
    long userId;

    private boolean canDrawHide = false;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GourdChildProto.CrossGourdChildDrawHideBoxReqMsg reqMsg = GourdChildProto.CrossGourdChildDrawHideBoxReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        serverId = reqMsg.getServerId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        GourdChildActivity activity = CrossGourdChildMgr.getGourdChildActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_NO_OPEN;
            return;
        }

        canDrawHide = activity.drawHideBox();
    }

    @Override
    public void sendData() {
        GourdChildProto.CrossGourdChildDrawHideBoxRespMsg.Builder builder = GourdChildProto.CrossGourdChildDrawHideBoxRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.setIsHide(canDrawHide);
        }
        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(GameProtocol.S_GOURD_CHILD_DRAW_HIDE_BOX_FROM_CROSS, builder));
    }
}
